// pages/vehicle/vehicle.js
const {request} = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: wx.getStorageSync('sysname')+"-统计查询",
    value:0,
    dayStarVisible: false,
    dayStar: new Date('2021-12-23').getTime(),
    dayStarText: '请选择选择时间',
    dayEndVisible: false,
    dayEnd: new Date('2021-12-23').getTime(),
    dayEndText: '请选择选择时间',
    nightStarVisible: false,
    nightStar: new Date('2021-12-23').getTime(),
    nightStarText: '请选择选择时间',
    nightEndVisible: false,
    nightEnd: new Date('2021-12-23').getTime(),
    nightEndText: '请选择选择时间',
    checked:0,
    host:[],
    value1:0,
    vehicleUsage:[],
    vehicleUsageGroup:[],
    total:-1,
    page:1,
    pages:1,
    hostCheckbox:[]

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this
    request('GET', 'api.VehicleData/searchData', {}).then(res => {
      console.log("searchData",res)

      if(res.code==1){
        let data = res.data
        that.setData({
          dayStar:new Date(data.day_star).getTime(),
          dayStarText:data.day_star,
          dayEnd:new Date(data.day_end).getTime(),
          dayEndText:data.day_end,
          nightStar:new Date(data.night_star).getTime(),
          nightStarText:data.night_star,
          nightEnd:new Date(data.night_end).getTime(),
          nightEndText:data.night_end,
          host:data.hosts
        })
      }else{
        wx.showToast({
          title: res.info,
          icon: 'none',
          duration: 2000
        })
      }
    })

  },
  submit(){
    this.setData({
      page:1,
      pages:1,
      vehicleUsage:[]
    })
    this.getvehicleUsageGroup()
    this.getvehicleUsage()
  },
  getvehicleUsageGroup(){
    wx.showLoading({
      title: '加载中',
    })
    let that = this
    request('POST', 'api.VehicleData/getVehicleUsageGroup', {
      host: that.data.hostCheckbox,
      time_type:that.data.value,
      day_star_time:that.data.dayStarText,
      day_end_time:that.data.dayEndText,
      night_star_time:that.data.nightStarText,
      night_end_time:that.data.nightEndText,
    }).then(res => {
      console.log("getVehicleUsageGroup",res)
      if(res.code==1){
        let data = res.data
        that.setData({
          vehicleUsageGroup:data.data,
          total:data.total
        })
      }else{
        wx.showToast({
          title: res.info,
          icon: 'none',
          duration: 2000
        })
      }
      wx.hideLoading()
    })
  },

  getvehicleUsage(){
    let that = this
    let page = that.data.page
    let vehicleUsage = that.data.vehicleUsage
    request('POST', 'api.VehicleData/getVehicleUsage', {
      host: that.data.hostCheckbox,
      time_type:that.data.value,
      day_star_time:that.data.dayStarText,
      day_end_time:that.data.dayEndText,
      night_star_time:that.data.nightStarText,
      night_end_time:that.data.nightEndText,
      page:page
    }).then(res => {
      console.log("getVehicleUsage",res)
      if(res.code==1){

        let data = res.data.data
        vehicleUsage = vehicleUsage.concat(data)
        that.setData({
          vehicleUsage:vehicleUsage,
          page:page+1,
          pages:res.data.last_page
        })
      }else{
        wx.showToast({
          title: res.info,
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

  bindscrolltolower(){
    console.log('scrolltolower')
    if(this.data.value1==1){
      console.log('scrolltolower111')
      if(this.data.page>this.data.pages){
        wx.showToast({
          title: '没有更多了',
          icon: 'none',
          duration: 1000
        })
        return
      }else{
        this.getvehicleUsage()
      }
    }


  },


  onChange(e) {
    console.log("checkbox",e)
    this.setData({
      hostCheckbox: e.detail.value,
    });
  },

  onTabsChange(event) {
    console.log(`Change tab, tab-panel value is ${event.detail.value}.`);
    this.setData({
      value: event.detail.value
    })
  },
  onTabsChange1(event) {
    console.log(`Change tab, tab-panel value is ${event.detail.value}.`);
    this.setData({
      value1: event.detail.value
    })
  },
  showPicker(e) {
    const { mode } = e?.currentTarget?.dataset;
    this.setData({
      mode,
      [`${mode}Visible`]: true,
    });
  },
  hidePicker() {
    const { mode } = this.data;
    this.setData({
      [`${mode}Visible`]: false,
    });
  },
  onConfirm(e) {
    const { value } = e?.detail;
    const { mode } = this.data;

    console.log('confirm', value);

    this.setData({
      [mode]: value,
      [`${mode}Text`]: value,
    });

    this.hidePicker();
  },

  onColumnChange(e) {
    console.log('pick', e?.detail?.value);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})